package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Gui.DiceImagePanel;
import Gui.GameFrame;
import Main.Defines;

public class RollDiceDialog extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8817680130619560783L;
	private int NewDay;
	private JPanel jp;
	public RollDiceDialog() {
		setModalityType(getModalityType().APPLICATION_MODAL);
		NewDay = 0;
		jp = new JPanel();
		jp.setBorder(Defines.getEmptyBorderConstraints());
		jp.setLayout(Defines.getHeightStackedBorderLayout());
		
		Display();
		add(jp);
		setResizable(false);
		pack();
		
		if(getTitle() != null){
			
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH;
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
			
		}
		setLocationRelativeTo(GameFrame.getInstance());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		
	}

	public void Display() {
		Defines.GM.RollDice();
		NewDay = Defines.CP().getDay() + Defines.GM.getDice1();
		if (NewDay > 31)
		{
			NewDay = 31;
		}
		JLabel value = new JLabel("You rolled a " + Defines.GM.getDice1() + "!");
		jp.add(value, BorderLayout.NORTH);
		try {
			JPanel dicePanel = new JPanel();
			dicePanel.add(new DiceImagePanel().getDicePanel(true, Defines.GM.getDice1()));
			jp.add(dicePanel, BorderLayout.CENTER);
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DrawButtons();
	}

	public void DrawButtons() {
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Defines.CP().setDay(NewDay);
				GameFrame.getInstance().update();
				Defines.EVM.queueEvent("EVENT_HANDLE_DAY");
				
				dispose();
				
			}
		});
		jp.add(ok, BorderLayout.SOUTH);
	}
}
